package com.cafuc.discipline.service.impl;

import com.cafuc.discipline.dao.UserDao;
import com.cafuc.discipline.entity.User;
import com.cafuc.discipline.respone.Result;
import com.cafuc.discipline.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description:
 * @Auther: Mete
 * @Date: 2022/11/15
 */

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userdao;

    /**
     * @Description: 用户登录
     * @Auther: Mete
     * @Date: 2022/11/19
     */

    @Override
    public Result login(HashMap<String, String> params, String ip, String ug) {
        User user = userdao.selectUserByEmail(params.get("email"));
        HashMap info = new HashMap();
        if(user==null)
            return Result.failure("用户不存在");
        if(!user.getPassword().equals(params.get("password")))
            return Result.failure("密码错误");
        user.setPassword("");
        /*此处后续添加token验证*/
        info.put("addr",ip);
        info.put("token","此处后续添加token验证");
        info.put("user_id",user.getId());
        info.put("time",new Date());
        info.put("device",ug);

        userdao.insertLoginLog(info);

        return Result.success(info,"登录成功");
    }


    /**
     * @Description: 新增用户
     * @Auther: Mete
     * @Date: 2022/11/23
     */

    @Override
    public Result insertNewUser(HashMap<String, Object> params) {
        if(params.get("email")==null)
            return Result.failure("邮箱为空");
        User user = userdao.selectUserByEmail(params.get("email").toString());
        if(user!=null)
            return Result.failure("用户邮箱已经存在");
        Boolean isOk = userdao.insertNewUser(params);
        if(isOk)
            return Result.success("ok");
        return Result.failure("新增出错");
    }
    /**
     * @Description: 查询用户列表
     * @Auther: Mete
     * @Date: 2023/1/8
     */
    @Override
    public Result selectUserList(HashMap<String,Object> params) {
        int total = 0;
        List<User> data = null;
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        int pageNum = (Integer.parseInt(params.get("pageIndex").toString())  - 1) * pageSize;
        params.put("pageNum",pageNum);
        params.put("pageSize",pageSize);
        if (params.containsKey("name")||params.containsKey("department")||params.containsKey("role")){
            total = userdao.queryUserTotal(params);
            data = userdao.queryUser(params);
        }else{
            total = userdao.selectUserTotal();
            data = userdao.selectUserList(params);
        }



        return Result.success(data,Integer.parseInt(params.get("pageIndex").toString()),pageSize,total);
    }
    /**
     * @Description: 查询当前用户信息
     * @Auther: Mete
     * @Date: 2022/11/23
     */
    @Override
    public Result selectUserNow(String userId) {
        User user = userdao.selectUserById(userId);
        user.setPassword(null);
        if(user==null)
            return Result.failure("用户不存在");
        return Result.success(user);
    }

    /**
     * @Description:用户信息删除
     * @Auther: Mete
     * @Date: 2022/12/06
     */

    @Override
    public Result deleteUser(String id) {
        User user = userdao.selectUserById(id);
        if(user==null)
            return Result.failure("用户不存在");
        Boolean isOk = userdao.deleteUser(id);
        if(isOk)
            return Result.success();
        return Result.failure();
    }

    /**
     * @Description: 查询用户登录日志
     * @Auther: Mete
     * @Date: 2022/12/06
     */
    @Override
    public Result selectUserLog(String user_id) {
        return Result.success(userdao.selectUserLog(user_id));
    }
}
